﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using Weather.Models;

namespace Weather.Services
{
    public class dbdao
    {

        public IDbConnection LoadFromFile()
        {
          
            database db = new database();
            //读取xml文档(设计过程中为了便于修改配置因此使用xml配置数值)
            XmlSerializer serializer = new XmlSerializer(typeof(database));

           FileStream fs = new FileStream(@"C:\MySoftware\文档\Weather\Weather\config\dbConfig.xml", FileMode.Open);
            try
            {
                db = (database)serializer.Deserialize(fs);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            
            Console.WriteLine("ip:" + db.ip + "\n" + "Servername:" + db.serverName);

            //连接数据库
            string conString;
            conString = string.Format("Data Source={0},1433;Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}",db.ip,db.serverName,db.username,db.password);
            //conString = "Data Source=118.190.45.106,1433;Initial Catalog=ForTest;Persist Security Info=True;User ID=sbx;Password=test";
                IDbConnection con = new SqlConnection(conString);
            //必须关闭文件，不关闭下次访问就会访问不到
            fs.Close();
            
            return con;
        }
    }
}